package com.azlogica.control;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.azlogica.dao.FrameDaoAdapter;
import com.azlogica.entity.Frame;

public class ManageFrame {
	private FrameDaoAdapter frameDao;

	public ManageFrame(Context context) {
		frameDao = new FrameDaoAdapter(context);
		frameDao.open();
		frameDao.close();
	}

	public void CreateFrame(String header, String report, String sack,
			String messType, String imei, String status, String number,
			String accuracy, String speed, String azimuth, String altitude,
			String longitude, String latitude, String gpsUtc, String mcc,
			String mnc, String lac, String cllId, String battery,
			String sendTime) {
		frameDao.open();
		frameDao.createFrame(header, report, sack, messType, imei, status,
				number, accuracy, speed, azimuth, altitude, longitude,
				latitude, gpsUtc, mcc, mnc, lac, cllId, battery, sendTime);
		frameDao.close();
	}

	public ArrayList<Frame> readAllFrames() {
		frameDao.close();
		Cursor cursor = frameDao.readFrame();
		ArrayList<Frame> frames = new ArrayList<Frame>();
		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				String header = cursor.getString(cursor
						.getColumnIndex("frame_header"));
				String report = cursor.getString(cursor
						.getColumnIndex("frame_report"));
				String sack = cursor.getString(cursor
						.getColumnIndex("frame_sack"));
				String mssType = cursor.getString(cursor
						.getColumnIndex("frame_messtype"));
				String imei = cursor.getString(cursor
						.getColumnIndex("frame_imei"));
				String status = cursor.getString(cursor
						.getColumnIndex("frame_status"));
				String number = cursor.getString(cursor
						.getColumnIndex("frame_number"));
				String accuracy = cursor.getString(cursor
						.getColumnIndex("frame_accuracy"));
				String speed = cursor.getString(cursor
						.getColumnIndex("frame_spped"));
				String azimuth = cursor.getString(cursor
						.getColumnIndex("frame_azimuth"));
				String altitude = cursor.getString(cursor
						.getColumnIndex("frame_altitude"));
				String longitude = cursor.getString(cursor
						.getColumnIndex("frame_longitude"));
				String latitude = cursor.getString(cursor
						.getColumnIndex("frame_latitude"));
				String gpsUtc = cursor.getString(cursor
						.getColumnIndex("frame_gps_utc"));
				String mcc = cursor.getString(cursor
						.getColumnIndex("frame_mcc"));
				String mnc = cursor.getString(cursor
						.getColumnIndex("frame_mnc"));
				String lac = cursor.getString(cursor
						.getColumnIndex("frame_lac"));
				String cllId = cursor.getString(cursor
						.getColumnIndex("frame_cllId"));
				String battery = cursor.getString(cursor
						.getColumnIndex("frame_battery"));
				String sendTime = cursor.getString(cursor
						.getColumnIndex("frame_sendTime"));
				String serial = cursor.getString(cursor
						.getColumnIndex("frame_serial"));
				int send = cursor.getInt(cursor.getColumnIndex("frame_send"));
				frames.add(new Frame(id, header, report, sack, mssType, imei,
						status, number, accuracy, speed, azimuth, altitude,
						longitude, latitude, gpsUtc, mcc, mnc, lac, cllId,
						battery, sendTime, serial, send));
			} while (cursor.moveToNext());
		}
		cursor.close();
		frameDao.close();
		return frames;
	}

	public String buildFrame(Frame frame) {
		String frameResult;
		frameResult = frame.getHeader() + frame.getReport() + frame.getSack()
				+ frame.getMessType() + frame.getImei() + frame.getNumber()
				+ frame.getAccuracy() + frame.getSpeed() + frame.getAzimuth()
				+ frame.getAltitude() + frame.getLongitude()
				+ frame.getLatitude() + frame.getGpsUTC() + frame.getMcc()
				+ frame.getMnc() + frame.getLac() + frame.getCllId()
				+ frame.getBattery() + frame.getSendTime();
		frameResult = separateFrame(frameResult);
		frameResult = removeLeadingZeros(frameResult);
		return frameResult;
	}

	public String separateFrame(String frame) {
		int interval = 2;
		char separator = ' ';

		StringBuilder sb = new StringBuilder(frame);

		for (int i = 0; i < frame.length() / interval; i++) {
			sb.insert(((i + 1) * interval) + i, separator);
		}

		return frame = sb.toString();
	}

	public String removeLeadingZeros(String frame) {
		int interval = 2;
		String temp = frame;
		frame = "";
		for (int i = 0; i < temp.length() - interval; i = i + 3) {
			frame += " "
					+ temp.substring(i, i + 2).replaceFirst("^0+(?!$)", "");
		}
		return frame = frame.substring(1, frame.length());
	}
}
